﻿using System.Collections.Generic;
using System.Linq;
using ARMIR.util;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
using Verse.AI;
using static UnityEngine.GraphicsBuffer;

namespace ARMIR {
    /// <summary>
    /// 余震子弹，定义了战术装备武器射出的子弹拥有余震爆炸效果
    /// </summary>
    public class ARMIRAfterShockBullet : Bullet {
        protected override void Impact(Thing hitThing, bool blockedByShield = false) {
            base.Impact(hitThing, blockedByShield);
            //Log.Message("[DEBUG] 子弹Impact方法被调用"); // 调试日志1

            if (base.Map == null) {
                //Log.Warning("地图不存在，无法爆炸");
                Map mapTryFind = this.Map ?? Find.CurrentMap; // 尝试获取当前地图
                if (mapTryFind == null) {
                    Log.Warning("地图不存在，无法爆炸");
                    return; // 仍然无效则放弃
                }
            }

            Map map = this.Map ?? Find.CurrentMap;

            int powerBase = (int)ARMIR_ModSettings.powerSet;

            List<Thing> ExplosiveIgnoredPawns = new List<Thing>();
            ExplosiveIgnoredPawns = map.mapPawns.AllPawnsSpawned.Where(p => p.Faction == Faction.OfPlayer && p.Position.DistanceTo(base.Position) < 4).OfType<Thing>().ToList();
            ExplosiveIgnoredPawns.AddRange(map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.HaulableEver)).Where(p => p.Position.DistanceTo(base.Position) < 4));
            ExplosiveIgnoredPawns.AddRange(map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.Corpse)).Where(p => p.Position.DistanceTo(base.Position) < 4));
            ExplosiveIgnoredPawns.Add(this.launcher);

            GenExplosion.DoExplosion(
                center: base.Position,
                map: map,
                radius: 1.5f,
                damType: DamageDefOf.Crush,
                instigator: this.launcher,
                damAmount: 24 * powerBase,
                armorPenetration: 0.4f * powerBase,
                screenShakeFactor: 0.3f, //屏幕震动强度(默认为1
                explosionSound: DefDatabase<SoundDef>.GetNamed("ARMIR_NormalHit"),
                ignoredThings: ExplosiveIgnoredPawns
            );

            //针对建筑物破坏的攻击
            ExplosiveIgnoredPawns.AddRange(map.mapPawns.AllPawnsSpawned.Where(p => p.Position.DistanceTo(this.Position) < 4));

            GenExplosion.DoExplosion(
                center: base.Position,
                map: map,
                radius: 1.5f,
                damType: DamageDefOf.Bomb,
                instigator: this.launcher,
                damAmount: 80,
                armorPenetration: 0.8f,
                screenShakeFactor: 0f, //屏幕震动强度(默认为1
                explosionSound: DefDatabase<SoundDef>.GetNamed("ARMIR_fakeEmpty"),
                ignoredThings: ExplosiveIgnoredPawns
            );

        }
        protected override void Tick() {
            base.Tick();
            EffecterDef bulletDrag = DefDatabase<EffecterDef>.GetNamed("ARMIR_TMZeroOne_Effecter"); //子弹拖尾特效
            bulletDrag.Spawn(this.Position, this.Map);
        }
        
    }
    
}